import 'package:get/get.dart';
import 'package:xvideos/hj_model/video_model.dart';
import 'package:xvideos/hj_utils/api_service/mine_service.dart';
import 'package:xvideos/hj_utils/global.dart';
import 'mine_publish_sub_logic.dart';
class MinePublishLogic extends GetxController {
  MinePublishLogic();

  List<VideoModel> checkedList = [];

  List<VideoModel> dataSource = [];

  void addChecked(VideoModel videoModel) {
    checkedList.add(videoModel);
    update();
  }

  void removeChecked(VideoModel videoModel) {
    checkedList.remove(videoModel);
    update();
  }

  void clearChecked() {
    checkedList.clear();
    update();
  }

  bool get isAllChecked {
    if (dataSource.isEmpty || checkedList.isEmpty) {
      return false;
    }
    return checkedList.length == dataSource.length;
  }

  bool get isEmptyChecked {
    return checkedList.isEmpty;
  }

  void selectAllOrNot() {
    if (isAllChecked) {
      checkedList.clear();
    } else {
      checkedList.clear();
      checkedList.addAll(dataSource);
    }
    update();
  }

  void delete() async {
    if (checkedList.isEmpty) {
      showToast('请选择要删除的项');
      return;
    }
    String resultMsg = await MineService.deleteMyPublishes(ids: checkedList.map((e) => e.id ?? '').toList());
    if (resultMsg.isEmpty) {
      checkedList.clear();
      update();
      Get.find<MinePublishSubLogic>(tag: '2').fetchPageData(isRefresh: true);
      showToast('删除成功');
    } else {
      showToast(resultMsg);
    }
  }
}
